/**
 instanceof
 通常来讲，使用 instanceof 就是判断一个实例是否属于某种类型。
 左边操作数是一个对象，右边操作数是一个函数对象或者函数构造器。
 原理是通过判断左操作数的对象的原型链上是否具有右操作数的构造函数的prototype属性。

 a instanceof b?alert("true"):alert("false")
 注意b值是你想要判断的那种数据类型，不是一个字符串，比如Array。
 **/

//怎么实现instanceof
function _instanceof(L, R) { //L为instanceof左表达式，R为右表达式
    let Ro = R.prototype //原型
    L = L.__proto__ //隐式原型
    while (true) {
        if (L === null) { //当到达L原型链顶端还未匹配，返回false
            return false
        }
        if (L === Ro) { //全等时，返回true
            return true
        }
        L = L.__proto__
    }

}

console.log(Number instanceof Number) // false
console.log(String instanceof String) // false
console.log(Object instanceof Object) // true
console.log(Function  instanceof Function ) // true
console.log(Function  instanceof Object ) // true
console.log(Object  instanceof Function ) // true

function Foo(){}
var foo = new Foo()
console.log(Foo  instanceof Function ) // true
console.log(Foo  instanceof Foo ) // false
console.log(foo  instanceof Foo ) // true



console.log('-----')
console.log([] instanceof Array)  //true
console.log([] instanceof Object)  //true
console.log([] instanceof Function)  //false
console.log(null instanceof Object)  //false

console.log('-----')
let simpleStr = 'This is a simple string'
let myString  = new String()
let newStr    = new String('String created with constructor')
let myDate    = new Date()
let myObj     = {}
let myNonObj  = Object.create(null)

console.log(simpleStr instanceof String)  // returns false, string literal is not an object
console.log(myString instanceof String)  // returns true
console.log(newStr instanceof String)  // returns true
console.log(myString instanceof Object)  // returns true

console.log(myObj    instanceof Object )  // returns true, every object literal has Object.prototype as prototype
console.log(({})     instanceof Object )  // returns true, same case as above
console.log(myNonObj instanceof Object)  // returns false, prototype is end of prototype chain (null)

console.log(myString instanceof Date  )   // returns false

console.log(myDate instanceof Date  )    // returns true
console.log(myDate instanceof Object )   // returns true
console.log(myDate instanceof String)    // returns false